package com.zzyl.controller.security;

import com.zzyl.base.PageBean;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    RoleService roleService;

    @ApiOperation("角色分页")
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageBean<RoleVo>> page(@PathVariable Integer pageNum,
                                                 @PathVariable Integer pageSize,
                                                 @RequestBody RoleDto roleDto) {
        return ResponseResult.success(roleService.page(pageNum, pageSize, roleDto));
    }


    @ApiOperation("角色添加")
    @PutMapping
    public ResponseResult<Void> createRole(@RequestBody RoleDto roleDto) {
        log.info("角色添加：{}", roleDto);
        roleService.createRole(roleDto);
        return ResponseResult.success();
    }

    @ApiOperation("根据角色查询选中的资源数据")
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<List<String>> findCheckedResources(@PathVariable Long roleId) {
        return ResponseResult.success(roleService.findCheckedResources(roleId));

    }

    @ApiOperation("角色修改")
    @PatchMapping
    public ResponseResult<Void> updateRole(@RequestBody RoleDto roleDto) {
        roleService.updateRole(roleDto);
        return ResponseResult.success();
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/{roleIds}")
    public ResponseResult<Void> deleteRole(@PathVariable Long roleIds) {
        roleService.deleteRole(roleIds);
        return ResponseResult.success();
    }

    @ApiOperation("查询所有角色")
    @PostMapping("/init-roles")
    public ResponseResult<List<Role>> list() {
        return ResponseResult.success(roleService.list());
    }
}